import { defineConfig, loadEnv } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import path from "path"

// https://vitejs.dev/config/
let build = {
  chunkSizeWarningLimit: 1500,
  rollupOptions: {
    output: {
      manualChunks(id) {
        if (id.includes('node_modules')) {
          return id.toString().split('node_modules/.pnpm/')[1].split('/')[0].toString();
        }
      }
    }
  }
}

export default defineConfig(({ mode }) => {
  const ENV = loadEnv(mode, process.cwd())
  return {
    plugins: [
      uni(),
      AutoImport({
        include: [/\.[tj]sx?&/, /\.vue$/, /\.vue\?vue/],
        imports: ["vue", "uni-app", "pinia"]
      }),
      Components({ dirs: ["src/components"] })
    ],
    resolve: { alias: { "@": path.resolve(__dirname, "./src") } },
    build,
    server: {
      host: true,
      port: 8088,
      strictPort: false,
      cors: true,
      https: false,
    },
    proxy: {
      [ENV.VITE_APP_BASE_API]: {
        target: ENV.VITE_APP_BASE_URL,
        changeOrigin: true,
        secure: false,
        ws: true,
        rewrite: (path) => path.replace(new RegExp(`${ENV.VITE_APP_BASE_API}`, "g"), " ")
      }
    }
  }
});
